package com.example.zx_springboot.service.iml;

import com.example.zx_springboot.entity.ZxRole;
import com.example.zx_springboot.entity.ZxUser;
import com.example.zx_springboot.mapper.ZxUserMapper;

import com.example.zx_springboot.mapper.Zx_RoleMapper;
import com.example.zx_springboot.service.ZxUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@CacheConfig(cacheNames = "user")//缓存名，和管理器中配置的一致
public class UseService implements ZxUserService {
    @Autowired
    private ZxUserMapper userMapper;

    @Autowired
    private Zx_RoleMapper zxRoleMapper;

    @Cacheable(key = "#id")
    @Override

    public ZxUser getUserById(int id){
        System.out.println("查询"+id+"号员工");
        ZxUser zxUser =userMapper.getUserByid(id);
        return zxUser;
    }
    @Override
    public ZxUser UpdateUser(ZxUser zxUser) {
        System.out.println("updateuser:"+ zxUser);
        userMapper.UpdateUser(zxUser);
        return zxUser;
    }

    @Override
    public List<ZxUser>  selectname(String userName) {

        return userMapper.SelectName(userName);
    }

    @Override
    public void deleteuser(Integer user_id) {
        userMapper.DeleteUser(user_id);
        System.out.println("deleteuser:"+user_id);
    }

    @Override
    public void deleteusers(List<Integer> ids) {
        for (Integer user_id : ids) {
            // 进行删除操作，可以调用你的删除方法
            userMapper.DeleteUser(user_id);
        }
    }


    @Override
    public ZxUser login(ZxUser zxUser) {
        //根据接收用户名密码查询数据库
        ZxUser zxUserDB = userMapper.login(zxUser);

        return zxUserDB;
    }

    @Override
    public void adduser(ZxUser zxUser) {
        userMapper.AddUser(zxUser);


    }

    @Override

    public PageInfo findByPage(Integer pageNum, Integer pageSize,String userName) {
        // 设置分页 页数和 页面最大数量，会自动达到分页的效果
        Page<ZxUser> page = PageHelper.startPage(pageNum, pageSize);
        //查询出来所有内容，会根据上面设置的分页参数显示
        if(userName!="") {
            userName = "%" + userName + "%";
            List<ZxUser> zxUsers = userMapper.SelectName(userName);
            PageInfo<ZxUser> userPageInfo = new PageInfo<>(zxUsers);
            System.out.println("当前页"+userPageInfo.getPageNum());
            System.out.println("总页数"+userPageInfo.getPages());
            System.out.println("页大小"+userPageInfo.getSize());
            System.out.println("总记录数"+userPageInfo.getTotal());
            System.out.println("当前页数据"+userPageInfo.getList());

            return userPageInfo;
        }else {
            List<ZxUser> zxUsers = userMapper.getall();
            PageInfo<ZxUser> userPageInfo = new PageInfo<>(zxUsers);
            System.out.println("当前页"+userPageInfo.getPageNum());
            System.out.println("总页数"+userPageInfo.getPages());
            System.out.println("页大小"+userPageInfo.getSize());
            System.out.println("总记录数"+userPageInfo.getTotal());
            System.out.println("当前页数据"+userPageInfo.getList());

            return userPageInfo;
        }


        //给前端页面返回分页信息,在前端进行显示




    }
    @Cacheable(key = "'ids'")
    @Override
    public List<ZxUser> getall() {
        List<ZxUser> zxUsers =userMapper.getall();
        return zxUsers;
    }

    @Override
    public ZxUser isregister(ZxUser zxUser) {
        return userMapper.RegisterName(zxUser);
    }

    @Override
    public ZxUser person(String userName) {
        return userMapper.PersonName(userName);
    }

    @Override
    public void Updateperson(ZxUser zxUser) {
         userMapper.Updateperson(zxUser);
    }

    @Override
    public ZxUser personpwd(ZxUser zxUser) {
        return userMapper.PersonPwd(zxUser);
    }

    @Override
    public void Updatepersonpwd(Map<String, String> paramMap) {
        userMapper.Updatepersonpwd(paramMap);
    }




}
